<template>
  <div style="width: 70%; margin: 20px auto">
    <div style="display: flex; grid-gap: 50px">
      <div style="width: 200px">
        <div style="text-align: center">
          <img :src="data.singerData.avatar" alt="" style="width: 150px; height: 150px; border-radius: 50%">
          <div style="font-size: 18px">{{ data.singerData.name }}</div>
        </div>
        <div style="margin-top: 30px"><span style="color: #999999">出生</span>：{{ data.singerData.birth }}</div>
        <div style="margin-top: 5px"><span style="color: #999999">地区</span>：{{ data.singerData.area }}</div>
        <div style="margin-top: 5px"><span style="color: #999999">性别</span>：{{ data.singerData.sexy }}</div>
        <div style="margin-top: 5px"><span style="color: #999999">热度</span>：🔥{{ data.singerData.hot }}</div>
        <div style="margin-top: 5px"><span style="color: #999999">代表作</span>：{{ data.singerData.master }}</div>
        <div style="margin-top: 10px; color: #666666; text-align: justify"><span style="color: #999999">简介</span>：<span style="font-size: 12px">{{ data.singerData.description }}</span></div>
      </div>
      <div style="flex: 1">
        <div style="display: flex; grid-gap: 50px; align-items: center">
          <div style="flex: 1; font-size: 22px; margin-top: 20px">歌曲列表（{{ data.songData.length }}）</div>
          <div style="flex: 1; text-align: right">
            <el-button type="success" :disabled="!data.songData.length" @click="playAll"><el-icon><VideoPlay /></el-icon><span style="margin-left: 5px">加入播放列表</span></el-button>
            <el-button type="warning" v-if="!data.collectData.length" @click="collectSinger"><el-icon><Star /></el-icon><span style="margin-left: 5px">收藏歌手</span></el-button>
            <el-button type="danger" v-else @click="disCollectSinger"><el-icon><StarFilled /></el-icon><span style="margin-left: 5px">取消收藏</span></el-button>
          </div>
        </div>
        <div style="margin-top: 30px">
          <el-table :data="data.songData" stripe>
            <el-table-column prop="name" label="名称" width="200" show-overflow-tooltip>
              <template v-slot="scope">
                <a :href="'/front/songDetail?id=' + scope.row.id" style="color: #1357be">{{ scope.row.name }}</a>
              </template>
            </el-table-column>
            <el-table-column prop="composer" label="作曲人" show-overflow-tooltip></el-table-column>
            <el-table-column prop="lyricist" label="作词人" show-overflow-tooltip></el-table-column>
            <el-table-column prop="time" label="发行时间" show-overflow-tooltip></el-table-column>
            <el-table-column prop="albumName" label="所属专辑" show-overflow-tooltip>
              <template v-slot="scope">
                <a :href="'/front/albumDetail?id=' + scope.row.albumId + '&singerId=' + scope.row.singerId" style="color: #1357be">《{{ scope.row.albumName }}》</a>
              </template>
            </el-table-column>
            <el-table-column prop="categoryName" label="所属分类" show-overflow-tooltip></el-table-column>
            <el-table-column prop="hot" label="热度">
              <template v-slot="scope">🔥{{ scope.row.hot }}</template>
            </el-table-column>
            <el-table-column label="" width="100" fixed="right">
              <template v-slot="scope">
                <div style="display: flex; align-items: center; grid-gap: 10px">
                  <el-icon style="cursor: pointer; color: #8c8b8b" size="20" v-if="!scope.row.collectId" @click="collectSong(scope.row.id)"><Star /></el-icon>
                  <el-icon style="cursor: pointer; color: #e1bc43" size="20" v-else @click="disCollectSong(scope.row.collectId)"><StarFilled /></el-icon>
                  <el-icon style="cursor: pointer; color: #8c8b8b" size="20" @click="addPlaylistInit(scope.row.id)"><FolderAdd /></el-icon>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="data.total" style="margin-top: 30px">
          <el-pagination @current-change="loadSongs" layout="total,prev, pager, next" :page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
        </div>
      </div>
    </div>
    <el-dialog title="我的歌单" v-model="data.formVisible" width="40%" destroy-on-close>
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="80px" style="padding: 20px">
        <el-form-item prop="playlistId" label="选择歌单">
          <el-select v-model="data.form.playlistId" placeholder="请选择歌单" style="width: 90%">
            <el-option v-for="item in data.playlistData" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <MusicPlayer :musicList="data.songData" v-if="data.songData?.length && data.playFlag" />
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
import {Delete, Edit} from "@element-plus/icons-vue";
import MusicPlayer from "@/views/components/MusicPlayer.vue";
const formRef = ref()
const data = reactive({
  user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
  singerId: router.currentRoute.value.query.id,
  singerData: {},
  songData: [],
  total: 0,
  pageNum: 1,
  pageSize: 15,
  name: null,
  collectData: [],
  playFlag: false,
  playlistData: [],
  form: {},
  formVisible: false,
  rules: {
    playlistId: [
      { required: true, message: '请选择歌单', trigger: 'blur' }
    ],
  }
})

const addPlaylistInit = (songId) => {
  request.get('/playlist/selectAll', {
    params: {
      userId: data.user.id
    }
  }).then(res => {
    if (res.code === '200') {
      data.playlistData = res.data
      data.form = {}
      data.form.songId = songId
      data.formVisible = true
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const save = () => {
  formRef.value.validate(valid => {
    if (valid) {
      request.post('/playlistItem/add', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success('加入成功')
          data.formVisible = false
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

const playAll = () => {
  data.playFlag = true
}

const loadSinger = () => {
  request.get('/singer/selectById/' + data.singerId).then(res => {
    if (res.code === '200') {
      data.singerData = res.data
      data.singerData.hot = data.singerData.hot + 1
      request.put('/singer/update', data.singerData)
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadSinger()

const loadSongs = () => {
  request.get('/sing/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      singerId: data.singerId
    }
  }).then(res => {
    if (res.code === '200') {
      data.songData = res.data?.list
      data.total = res.data?.total
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadSongs()

const collectSinger = () => {
  request.post('/collect/add', {
    relId: data.singerId,
    type: 'SINGER'
  }).then(res => {
    if (res.code === '200') {
      ElMessage.success('收藏成功')
      loadCollect()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const disCollectSinger = () => {
  request.delete('/collect/delete/' + data.collectData[0].id).then(res => {
    if (res.code === '200') {
      ElMessage.success('取消成功')
      loadCollect()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const loadCollect = () => {
  request.get('/collect/selectAll', {
    params: {
      userId: data.user.id,
      relId: data.singerId,
      type: 'SINGER'
    }
  }).then(res => {
    if (res.code === '200') {
      data.collectData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadCollect()

const collectSong = (songId) => {
  request.post('/collect/add', {
    relId: songId,
    type: 'SONG'
  }).then(res => {
    if (res.code === '200') {
      ElMessage.success('收藏歌曲成功')
      loadSongs()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const disCollectSong = (id) => {
  request.delete('/collect/delete/' + id).then(res => {
    if (res.code === '200') {
      ElMessage.success('取消成功')
      loadSongs()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

</script>